import { Body, Controller, Delete, Get, Post, Put, Query } from '@nestjs/common';
import {
  ApiOperation,
  ApiTags,
  ApiExcludeController,
  ApiBody,
  ApiBearerAuth,
  ApiExcludeEndpoint,
} from '@nestjs/swagger';

import { ApiResult } from '~/common/decorators/api-result.decorator';
import { IdParam } from '~/common/decorators/id-param.decorator';
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator';
import { Pagination } from '~/helper/paginate/pagination';
import { AuthUser } from '~/modules/auth/decorators/auth-user.decorator';
import { Perm, definePermission } from '~/modules/auth/decorators/permission.decorator';
import { DictTypeEntity } from '~/modules/system/dict-type/dict-type.entity';

import { DictTypeDto, DictTypeQueryDto } from './dict-type.dto';
import { DictTypeService } from './dict-type.service';
import { ModuleSwaggerTags } from '~/constants/modules.constant';
import { DictItemEntity } from '../dict-item/dict-item.entity';
import { AllowAnon } from '~/modules/auth/decorators/allow-anon.decorator';
import { Public } from '~/modules/auth/decorators/public.decorator';

export const permissions = definePermission('system:dict-type', {
  LIST: 'list',
  CREATE: 'create',
  READ: 'read',
  UPDATE: 'update',
  DELETE: 'delete',
} as const);

@ApiTags(ModuleSwaggerTags.DICT)
@ApiBearerAuth()
@Controller('label')
export class DictTypeController {
  constructor(private dictTypeService: DictTypeService) {}

  @ApiExcludeEndpoint()
  @Get()
  @ApiOperation({ summary: '获取字典类型列表' })
  @ApiResult({ type: [DictTypeEntity], isPage: true })
  @Perm(permissions.LIST)
  async list(@Query() dto: DictTypeQueryDto): Promise<Pagination<DictTypeEntity>> {
    return this.dictTypeService.page(dto);
  }

  @ApiExcludeEndpoint()
  @Get('select-options')
  @ApiOperation({ summary: '一次性获取所有的字典类型(不分页)' })
  @ApiResult({ type: [DictTypeEntity] })
  @Perm(permissions.LIST)
  async getAll(): Promise<DictTypeEntity[]> {
    return this.dictTypeService.getAll();
  }

  @ApiExcludeEndpoint()
  @Post()
  @ApiOperation({ summary: '新增字典类型' })
  @ApiBody({ type: DictTypeDto })
  // @Perm(permissions.CREATE)
  async create(@Body() dto: DictTypeDto, @AuthUser() user: IAuthUser): Promise<void> {
    await this.dictTypeService.isExistKey(dto.name);
    // dto.createBy = dto.updateBy = user.uid
    await this.dictTypeService.create(dto);
  }

  @ApiExcludeEndpoint()
  @Get(':id')
  @ApiOperation({ summary: '查询字典类型信息' })
  @ApiResult({ type: DictTypeEntity })
  @Perm(permissions.READ)
  async info(@IdParam() id: number): Promise<DictTypeEntity> {
    return this.dictTypeService.findOne(id);
  }

  @ApiExcludeEndpoint()
  @Post(':id')
  @ApiOperation({ summary: '更新字典类型' })
  @Perm(permissions.UPDATE)
  async update(
    @IdParam() id: number,
    @Body() dto: DictTypeDto,
    @AuthUser() user: IAuthUser
  ): Promise<void> {
    // dto.updateBy = user.uid
    await this.dictTypeService.update(id, dto);
  }

  @ApiExcludeEndpoint()
  @Delete(':id')
  @ApiOperation({ summary: '删除指定的字典类型' })
  @Perm(permissions.DELETE)
  async delete(@IdParam() id: number): Promise<void> {
    await this.dictTypeService.delete(id);
  }

  @ApiExcludeEndpoint()
  @Get('byCode')
  @ApiOperation({ summary: '根据字典类型编码查询字典类型' })
  async getByCode(@Query('code') code: string): Promise<Record<string, any>> {
    return this.dictTypeService.getByCode(code);
  }

	@Public()
  @Get('getByUser')
  @ApiOperation({ summary: '获取用户相关标签' })
  async getUserType(): Promise<Partial<DictTypeEntity>[]> {
    return this.dictTypeService.getUserType();
  }

	@AllowAnon()
  @Put('bindUser')
  @ApiOperation({ summary: '用户绑定标签' })
  async bindUser(@Query('uid') uid: number, @Query('labelIds') labelIds: string): Promise<void> {
    const labels = labelIds.split(',').map((item) => Number(item));
    await this.dictTypeService.bindUser(uid, labels);
  }
}
